package com.stylefeng.guns.wx.handler;

import com.stylefeng.guns.core.common.constant.state.ManagerStatus;
import com.stylefeng.guns.core.util.JacksonUtils;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.IDeptService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.wx.bean.WxContactXmlMsg;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class ContactHandler extends AbstractHandler {

    private IDeptService deptService;

    private IUserService userService;

    public void setDeptService(IDeptService deptService) {
        this.deptService = deptService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @Override
    public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) throws WxErrorException {
        String plainText = (String) context.get("plainText");
        try {
            WxContactXmlMsg wxContactXmlMsg = JacksonUtils.xmlToNormalObject(plainText, WxContactXmlMsg.class);

            //新增成员
            if (StringUtils.equals("create_user", wxContactXmlMsg.getChangeType())) {
                userService.syncWxUser(wxContactXmlMsg.getUserId(),null);
            }

            //更新成员
            if (StringUtils.equals("update_user", wxContactXmlMsg.getChangeType())) {
                userService.syncWxUser(wxContactXmlMsg.getUserId(),wxContactXmlMsg.getNewUserId());
            }
            //delete_user
            if (StringUtils.equals("delete_user", wxContactXmlMsg.getChangeType())) {
                User u = this.userService.getByWxUserId(wxContactXmlMsg.getUserId());
                this.userService.setStatus(u.getId(), ManagerStatus.DELETED.getCode());
            }

            //新增部门
            if (StringUtils.equals("create_party", wxContactXmlMsg.getChangeType())) {
                insertDept(wxContactXmlMsg);
            }

            //更新部门
            if (StringUtils.equals("update_party", wxContactXmlMsg.getChangeType())) {
                updateDept(wxContactXmlMsg);
            }

            //删除部门
            if (StringUtils.equals("delete_party", wxContactXmlMsg.getChangeType())) {
                deptService.deleteDept(wxContactXmlMsg.getId());
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    private void updateDept(WxContactXmlMsg wxContactXmlMsg) {
        Dept oldDept = deptService.selectById(wxContactXmlMsg.getId());
        if (oldDept == null) {
            insertDept(wxContactXmlMsg);
        } else {
            if (null != wxContactXmlMsg.getName()) {
                oldDept.setFullname(wxContactXmlMsg.getName());
                oldDept.setSimplename(wxContactXmlMsg.getName());
            }
            if (null != wxContactXmlMsg.getParentId()) {
                oldDept.setPid(wxContactXmlMsg.getParentId());
                deptSetPids(oldDept);
            }
            if (null != wxContactXmlMsg.getOrder()) {
                oldDept.setNum(wxContactXmlMsg.getOrder());
            }
            deptService.updateById(oldDept);
        }
    }

    private void insertDept(WxContactXmlMsg wxContactXmlMsg) {
        Dept dept = new Dept();
        dept.setId(wxContactXmlMsg.getId());
        dept.setFullname(wxContactXmlMsg.getName());
        dept.setSimplename(wxContactXmlMsg.getName());
        dept.setNum(wxContactXmlMsg.getOrder());
        dept.setPid(wxContactXmlMsg.getParentId());
        deptSetPids(dept);
        deptService.insert(dept);
    }

    private User toUser(WxContactXmlMsg wxContactXmlMsg) {
        User user = new User();

        return user;
    }


    private void deptSetPids(Dept dept) {
        if (ToolUtil.isEmpty(dept.getPid()) || dept.getPid().equals(0)) {
            dept.setPid(0);
            dept.setPids("[0],");
        } else {
            int pid = dept.getPid();
            Dept temp = deptService.selectById(pid);
            String pids = temp.getPids();
            dept.setPid(pid);
            dept.setPids(pids + "[" + pid + "],");
        }
    }


}
